clear
capture log close
set more off

version 10

/* --------------------------------------------------------------- *
    Description: Code runner for Financial Structure Database
    Author: Ed Al-Hussainy
 * --------------------------------------------------------------- */

* code directory
global codeloc "C:\temp\Structure\code\finstruct"

* data directory
global dataloc "C:\temp\Structure"

log using "$dataloc/log/codemaster", text replace

* update version
global dataver "NOV10"

* timeframe
global startyear 1980
global endyear 2009

* compete list of indicators 
#delimit;

global masterlist    "S01CGP0 S02ESS0 S01ESS0 S01IFS0 S06IFS0 S05BIS0 S01BSK0 S03BSK0 S04BSK0 
                     S05BSK0 S06BSK0 S07BSK0 S07IFS0 S08BSK0 S09IFS0 S03IFS0 S01FSI0 S02FSI0 
                     S03FSI0 S04FSI0 S10BSK0 S01BIS0 S02BIS0 S03BIS0 S04BIS0 S01WFE0 S02WFE0 
                     S03WDI0 S05WDI0 S01WDI0 S01AXC0 S01NBF0 S02AXC0 S02NBF0 S03NBF0";
                     
#delimit cr                     

/* --------------------------------------------------------------- *
   process data, generate metrics
 * --------------------------------------------------------------- */
 
cd "$codeloc"

* WDI, WEO  
do wdi_weo_metrics

* IFS 
do ifs_data
do gdp
do cpi
do ifs_metrics

* BankScope 
do bankscope_data
do bankscope_metrics

* BIS  
do bis_data
do bis_metrics

* AXCO 
do axco_data
do axco_metrics


/* --------------------------------------------------------------- *
   combine and process output
 * --------------------------------------------------------------- */

cd "$dataloc/output"

use WDImetrics, clear
merge cncode year using AXCmetrics BISmetrics BSKmetrics IFSmetrics, sort
drop _merge*

sort cncode

merge cncode using "$dataloc/data/CountryClassification2009", uniqusing
keep if _merge == 3
drop _merge

replace region = incgr if region == ".."

label var incgr "INCOME GROUP"
label var region "REGION"
label var cncode "WB COUNTRY CODE"
label var year "YEAR"

keep if year >= $startyear & year <= $endyear

* keep region incgr cn cncode year S*

isid cncode year
sort cncode year

compress
order incgr region cn cncode year

* Convert the Tier1 variables which are expressed in decimals into percentage:
local tier1_dec "S01BIS0 S01BSK0 S01IFS0 S01LMF0 S01NBF0 S01WDI0 S02BIS0 S02ESS0 S02NBF0 S03NBF0 S03WDI0 S04LMF0 S06IFS0 S08BSK0 S09IFS0 S11BSK0 S16BSK0"
   foreach i of var `tier1_dec' {
      replace `i'=`i'*100
      }

label data "Financial Indicators Master (Data version: $dataver)" 
save "$dataloc/Output/MasterData", replace

*** start ALLVARS.DO

tempfile temp1
use "$dataloc/data/Benchmark/EEIs&RHS.dta", clear
sort cncode year
foreach var of varlist agedep-offshore {
   rename `var' _`var'
}
save `temp1'

use "$dataloc/output/MasterData", clear

sort cncode year
merge cncode year using `temp1'
tab _merge
drop if _merge==2
drop _merge

sort cncode year
save "$dataloc/AllVars", replace

*** end ALLVARS.DO


/* end of code */
clear
log close

#delimit ;

                              .sssssssss.
                        .sssssssssssssssssss
                      sssssssssssssssssssssssss
                     ssssssssssssssssssssssssssss
                      @@sssssssssssssssssssssss@ss
                      |s@@@@sssssssssssssss@@@@s|s
               _______|sssss@@@@@sssss@@@@@sssss|s
             /         sssssssss@sssss@sssssssss|s
            /  .------+.ssssssss@sssss@ssssssss.|
           /  /       |...sssssss@sss@sssssss...|
          |  |        |.......sss@sss@ssss......|
          |  |        |..........s@ss@sss.......|
          |  |        |...........@ss@..........|
           \  \       |............ss@..........|
            \  '------+...........ss@...........|
             \________ .........................|
                      |.........................|
                     /...........................\
                    |.............................|
                       |.......................|
                           |...............|
                           
#delimit cr

di "Beer time!"
